package dimension.starry.entity;

import dimension.starry.constants.MonsterDamage;
import dimension.starry.constants.MonsterHealth;
import dimension.starry.utils.RandomUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.mob.HostileEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.dimension.DimensionTypes;

public class MonsterLevel {

    private static final String MONSTER_LEVEL = "MonsterLevel";

    public static void onLoad(Entity entity, ServerWorld world) {
        if (entity instanceof HostileEntity hostileEntity && DimensionTypes.OVERWORLD.equals(world.getDimensionKey())) {
            int level = RandomUtil.randomInt(1, 20);
            int maxHealth = MonsterHealth.getLevelHealth(level);
            int damage = MonsterDamage.getLevelDamage(level);
            hostileEntity.getAttributes()
                .getCustomInstance(EntityAttributes.GENERIC_MAX_HEALTH)
                .setBaseValue(maxHealth);
            hostileEntity.getAttributes()
                .getCustomInstance(EntityAttributes.GENERIC_ATTACK_DAMAGE)
                .setBaseValue(damage);
            hostileEntity.getAttributes()
                .getCustomInstance(Attributes.MONSTER_LEVEL.getAttribute())
                .setBaseValue(level);
            hostileEntity.setHealth(maxHealth);
        }
    }

    public static int getLevelFromEntity(LivingEntity livingEntity) {
        if (livingEntity instanceof HostileEntity) {
            return (int) livingEntity.getAttributeBaseValue(Attributes.MONSTER_LEVEL.getAttribute());
        }
        return 0;
    }

}
